//
//  LoginViewController.m
//  DailyRecord
//
//  Created by Gin on 2022/3/27.
//

#import "LoginViewController.h"
#import "UITextField+PlaceHolder.h"
#import "RegisterViewController.h"
#import "LoginParamModel.h"
#import "NetWorkService.h"
#import "UIImage+Resize.h"
#import "VevifyViewController.h"

@interface LoginViewController ()

@property (nonatomic, strong) UIImageView *logoImgView;

@property (nonatomic, strong) UILabel *accountLb;

@property (nonatomic, strong) UITextField *accountTf;

@property (nonatomic, strong) UILabel *pswLb;

@property (nonatomic, strong) UITextField *pswTf;

@property (nonatomic, strong) UIButton *loginBtn;

@property (nonatomic, strong) UILabel *toRegisterLb;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getAndBindData];
    [self setCustomNavigationTitle:@"登录"];
    [self addGes];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:YES];
}

- (void)initUI{
    [self.view addSubview:self.logoImgView];
    [self.view addSubview:self.accountLb];
    [self.view addSubview:self.accountTf];
    [self.view addSubview:self.pswLb];
    [self.view addSubview:self.pswTf];
    [self.view addSubview:self.loginBtn];
    [self.view addSubview:self.toRegisterLb];
}

- (void)setUpContraints{
    [self.logoImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(80, 80));
        make.top.mas_equalTo(100 +  kNavigationBarHeight);
    }];
    
    [self.accountLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.logoImgView.mas_left);
        make.top.equalTo(self.logoImgView.mas_bottom).offset(60);
    }];
    
    [self.accountTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.accountLb);
        make.height.equalTo(self.accountLb);
        make.left.equalTo(self.accountLb.mas_right).offset(kLeftSpace);
        make.width.mas_equalTo(100);
    }];
    
    [self.pswLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.accountLb);
        make.top.equalTo(self.accountLb.mas_bottom).offset(kBottomSpace);
    }];
    
    [self.pswTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.pswLb);
        make.left.width.height.equalTo(self.accountTf);
    }];
    
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pswLb.mas_bottom).offset(40);
        make.centerX.equalTo(self.view);
        make.left.mas_equalTo(kLeftSpace);
        make.height.mas_equalTo(kLargeButtonHeight);
    }];
    
    [self.toRegisterLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.loginBtn.mas_bottom).offset(kBottomSpace);
    }];
}

- (void)getAndBindData{
    self.logoImgView.image = [UIImage imageNamed:@"icon"];
    self.accountLb.text = @"账号 :";
    self.pswLb.text = @"密码 :";
    [self.loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    self.toRegisterLb.text = @"没有账号？请点击注册";
}

- (void)toLogin{
    LoginParamModel *paramM = [LoginParamModel new];
    paramM.lId = self.accountTf.text;
    paramM.passwd = self.pswTf.text;
    NSDictionary *param = [paramM yy_modelToJSONObject];
    [MBProgressHUD showMessage:kInRequestText toView:self.view];
    [NetWorkService servicePostWithPath:kUserLogin parameters:param success:^(id  _Nullable responseObj) {
        [MBProgressHUD hideHUDForView:self.view];
        User *user = [User new];
        user.username = self.accountTf.text;
        [[UserUntils sharedUserUntils] updateUser:user];
        //to main vc
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationLogined object:nil];
    } failure:^(NSError * _Nullable error) {
        [MBProgressHUD showError:kRequestFailText toView:self.view];
    }];
}

- (void)toRegister{
    RegisterViewController *registerVc = [RegisterViewController new];
    [self.navigationController pushViewController:registerVc animated:YES];
}

- (Boolean)verify{
    if (self.accountTf.text.length == 0) {
        [MBProgressHUD showMessage:@"请输入账号" toView:self.view];
        return NO;
    }else if (self.pswTf.text.length == 0){
        [MBProgressHUD showMessage:@"请输入密码" toView:self.view];
        return NO;
    }
    return YES;
}

- (void)addGes{
    UILongPressGestureRecognizer *ges = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGes:)];
    ges.minimumPressDuration = 3;
    [self.logoImgView addGestureRecognizer:ges];
}

- (void)longGes:(UILongPressGestureRecognizer *)ges{
    if (ges.state == UIGestureRecognizerStateEnded) {
        VevifyViewController *verifyVc = [VevifyViewController new];
        [self.navigationController pushViewController:verifyVc animated:YES];
    }
}

#pragma mark -- setter&&getter

- (UIImageView *)logoImgView{
    if (!_logoImgView) {
        _logoImgView = [UIImageView new];
        _logoImgView.userInteractionEnabled = YES;
    }
    return _logoImgView;
}

- (UILabel *)accountLb{
    if (!_accountLb) {
        _accountLb = [UILabel new];
        _accountLb.font = kInfoFont;
    }
    return _accountLb;
}

- (UITextField *)accountTf{
    if (!_accountTf) {
        _accountTf = [UITextField new];
        _accountTf.font = kInfoFont;
        _accountTf.placeholder = @"请输入账号";
        [_accountTf setPlaceholderFont:_accountTf.font];
    }
    return _accountTf;
}

- (UILabel *)pswLb{
    if (!_pswLb) {
        _pswLb = [UILabel new];
        _pswLb.font = kInfoFont;
    }
    return _pswLb;
}

- (UITextField *)pswTf{
    if (!_pswTf) {
        _pswTf = [UITextField new];
        _pswTf.font = kInfoFont;
        _pswTf.placeholder = @"请输入密码";
        [_pswTf setPlaceholderFont:_pswTf.font];
        [_pswTf setSecureTextEntry:YES];
    }
    return _pswTf;
}

- (UIButton *)loginBtn{
    if (!_loginBtn) {
        _loginBtn = [UIButton new];
        [_loginBtn.titleLabel setFont:kTitleFont];
        [_loginBtn setBackgroundImage:[[UIImage imageNamed:@"btn_bg"] resizeWithCenter]  forState:UIControlStateNormal];
        @weakify(self);
        [[_loginBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            Boolean isPassVerify = [self verify];
            if (isPassVerify) {
                [self toLogin];
            }
        }];
    }
    return _loginBtn;
}

- (UILabel *)toRegisterLb{
    if (!_toRegisterLb) {
        _toRegisterLb = [UILabel new];
        _toRegisterLb.font = kDescFont;
        _toRegisterLb.textColor = [UIColor blueColor];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toRegister)];
        _toRegisterLb.userInteractionEnabled = YES;
        [_toRegisterLb addGestureRecognizer:tap];
    }
    return _toRegisterLb;
}

@end
